// subpkg/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    historyList: [],
    searchRecord: [],
    notShow: false, 
    goodList: [],
    notshowEmpty: true, 
    notShowTitle:true
  },
  onSearch(e) {
    this.setData({
      value: e.detail,
      'notShow': true
    });
    var inputValue = this.data.value;
    var historyList = this.data.historyList || [];
    if (historyList.length < 5 && inputValue !== "") {
      historyList.unshift(inputValue);
    } else if (historyList.length >= 5 && inputValue !== "") {
      historyList.unshift(inputValue);
      historyList.pop();
    };
    wx.setStorageSync('historyList', historyList);
    this.searchProduct();
  },
  // 查询数据库数据
  searchProduct() {
    //console.log(this.data.value)
    let that = this;
    let key = this.data.value;
    const db = wx.cloud.database();
    const _ = db.command
    db.collection('product_details').where(_.or([{
      name: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },
    {
      synopsis: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    }
    ])).get({
      success: res => {
        //console.log(res.data.length);
        //console.log(that.data)
        if (res.data.length === 0) {
            that.setData({
              'notshowEmpty':false,
              'notShowTitle':false, 
              'goodList':''
            })
        } else {
          that.setData({
            'goodList': res.data,
            'notshowEmpty':true,

          })
        }
      },
      fail: err => {
        console.log(err);
      }
    })

  },
  // 点击历史记录自动填充
  fillWith(e) {
    // console.log(e.target.dataset.id)
    this.setData({
      'value':e.target.dataset.id,
      'notShow': true
    })
    // this.onSearch();
    this.searchProduct();
  },
  // 取消搜索时设置历史记录和热搜榜单的状态
  onCancel() {
    this.setData({
      'notShow': false,
      'notshowEmpty':true,
      'notShowTitle':false
    })
  },
  // 失去焦点时，读取缓存中的历史记录
  getHistory() {
    // console.log(this.data.value);
    this.setData({
      'searchRecord': wx.getStorageSync('historyList') || [],
    })
  },
  // 页面加载完成时，读取缓存中的历史记录，渲染到页面上
  onLoad() {
    let that = this
    wx.getStorage({
      key:'historyList',
      success(res){
        // console.log(res.data)
        // console.log(that.data)
        that.setData({
          'notShowTitle':false,
          'searchRecord': res.data
        })
      }
    })
    // v
  },
  delHistory() {
    wx.showModal({
      confirmColor: '#ff0000',
      content: "确定清除全部记录吗？",
      success: (res) => {
        if (res.confirm) {
          wx.removeStorage({
            key: 'historyList',
            success: (res) => {
              this.setData({
                'searchRecord': '',
                'value': '',
                'notShowTitle':true
              })
            }
          })
        }
      }
    })
  },
  goToDetail(e) {
    wx.navigateTo({
      url: '../productDetail/productDetail?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  }

})